﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class SysPropagate
    {
        public int Add(Chain.Model.SysPropagate model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SysPropagate(");
            builder.Append("SymbolID,PropagateDetail)");
            builder.Append(" values (");
            builder.Append("@SymbolID,@PropagateDetail)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SymbolID", SqlDbType.Int, 4), new SqlParameter("@PropagateDetail", SqlDbType.VarChar, 0x1388) };
            cmdParms[0].Value = model.SymbolID;
            cmdParms[1].Value = model.PropagateDetail;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int PropagateID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysPropagate ");
            builder.Append(" where PropagateID=@PropagateID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PropagateID", SqlDbType.Int, 4) };
            cmdParms[0].Value = PropagateID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
        public int Update(Chain.Model.SysPropagate model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysPropagate set ");
            builder.Append("SymbolID=@SymbolID,");
            builder.Append("PropagateDetail=@PropagateDetail");
            builder.Append(" where PropagateID=@PropagateID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SymbolID", SqlDbType.Int, 4), new SqlParameter("@PropagateDetail", SqlDbType.VarChar, 0x1388), new SqlParameter("@PropagateID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.SymbolID;
            cmdParms[1].Value = model.PropagateDetail;
            cmdParms[2].Value = model.PropagateID;
            if (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0)
            {
                return 1;
            }
            return 0;
        }
        public Chain.Model.SysPropagate GetModel(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 * from SysPropagate ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            Chain.Model.SysPropagate model = new Chain.Model.SysPropagate();
            DataSet set = DbHelperSQL.Query(builder.ToString());
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["PropagateID"] != null) && (set.Tables[0].Rows[0]["PropagateID"].ToString() != ""))
            {
                model.PropagateID = int.Parse(set.Tables[0].Rows[0]["PropagateID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SymbolID"] != null) && (set.Tables[0].Rows[0]["SymbolID"].ToString() != ""))
            {
                model.SymbolID = int.Parse(set.Tables[0].Rows[0]["SymbolID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["PropagateDetail"] != null) && (set.Tables[0].Rows[0]["PropagateDetail"].ToString() != ""))
            {
                model.PropagateDetail = set.Tables[0].Rows[0]["PropagateDetail"].ToString();
            }
          
            return model;
        }
    }
}

